package cn.iocoder.yudao.module.cms.feedback.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.feedback.dal.dataobject.FeedbackDO;
import cn.iocoder.yudao.module.cms.feedback.service.FeedbackService;
import cn.iocoder.yudao.module.cms.feedback.vo.AppFeedbackCreateReqVO;
import cn.iocoder.yudao.module.cms.feedback.vo.FeedbackPageReqVO;
import cn.iocoder.yudao.module.cms.feedback.vo.FeedbackRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 意见反馈")
@RestController
@RequestMapping("/cms/feedback")
@Validated
public class AppFeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @PostMapping("/create")
    @Operation(summary = "创建意见反馈")
    @PermitAll // 意见反馈可以不登录
    public CommonResult<Long> createFeedbackInfo(@Valid @RequestBody AppFeedbackCreateReqVO createReqVO) {
        return success(feedbackService.createFeedbackInfo(getLoginUserId(), createReqVO));
    }

    @GetMapping("/delete")
    @Operation(summary = "删除意见反馈")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFeedbackInfo(@RequestParam("id") Long id) {
        feedbackService.deleteFeedbackInfo(getLoginUserId(), id);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见反馈分页")
    public CommonResult<PageResult<FeedbackRespVO>> getFeedbackPage(@Valid FeedbackPageReqVO pageReqVO) {
        PageResult<FeedbackDO> pageResult = feedbackService.getFeedbackInfoPage(getLoginUserId(), pageReqVO);
        PageResult<FeedbackRespVO> data = BeanUtils.toBean(pageResult, FeedbackRespVO.class);
        return success(data);
    }

}
